package com.sc.sc250601.cook.controller;

import com.sc.sc250601.cook.pojo.BrowseRecord;
import com.sc.sc250601.cook.pojo.Recipe;
import com.sc.sc250601.cook.pojo.Result;
import com.sc.sc250601.cook.service.BrowseRecordService;
import com.sc.sc250601.cook.service.RecipeService;
import com.sc.sc250601.cook.util.UpDowns;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/recipe")
public class RecipeController {

    @Autowired
    private RecipeService recipeService;

    @Autowired
    private BrowseRecordService browseRecordService;

    /**
     * 根据分类（或名字）获取菜谱
     *
     * @param type
     * @return
     */
    @RequestMapping("/getRecipesByType")
    public Result getRecipesByType(String type) {
        if (type == null) {
            return new Result(0, "请输入搜索内容");
        }
        List<Recipe> recipes = recipeService.getRecipesByType(type);
        if (recipes == null || recipes.size() == 0) {
            return new Result(0, "暂无数据");
        }
        return new Result(1, "success", recipes);
    }

    /**
     * 周Top20
     *
     * @return
     */
    @RequestMapping("/weekTop")
    public Result weekTop() {
        List<Recipe> recipes = recipeService.weekTop();
        if (recipes == null) {
            return new Result(0, "暂无数据");
        }
        System.out.println(recipes);
        return new Result(1, "success", recipes);
    }

    /**
     * 昨日收藏Top10
     *
     * @return
     */
    @RequestMapping("/favoriteTop")
    public Result favoriteTop() {
        List<Recipe> recipes = recipeService.favoriteTop();
        if (recipes == null) {
            return new Result(0, "暂无数据");
        }
        return new Result(1, "success", recipes);
    }

    /**
     * 通过浏览记录推荐菜谱
     *
     * @return
     */
    @RequestMapping("/getRecipeListByUserId")
    public Result getRecipeListByUserId(Long userId) {
        List<Recipe> recipes = recipeService.getRecipeListByUserId(userId);
        // 当返回的集合为空时，获取默认菜谱
        if (recipes == null || recipes.isEmpty()) {
            List<Recipe> defaultRecipes = recipeService.getRecipes();
            return new Result(1, "success", defaultRecipes);
        }
        return new Result(1, "success", recipes);
    }

    /**
     * 未登录情况下，查询菜谱
     *
     * @return
     */
    @RequestMapping("/getRecipes")
    public Result getRecipes() {
        List<Recipe> recipes = recipeService.getRecipes();
        if (recipes == null) {
            return new Result(0, "暂无数据");
        }
        return new Result(1, "success", recipes);
    }

    /**
     * 点击菜谱后添加浏览次数，并且添加浏览记录
     *
     * @return
     */
    @RequestMapping("/addViewRecord")
    public Result addViewRecord(@RequestBody BrowseRecord browseRecord) {
        // 1. 更新菜谱浏览量
        int count = recipeService.addViewCount(browseRecord.getBrowseId());
        if (count <= 0) {
            return new Result(0, "浏览数量添加失败");
        }
        if (browseRecord.getUserId() != null) {
            int result = browseRecordService.recordBrowse(browseRecord);
            if (result <= 0) {
                return new Result(0, "添加浏览记录失败");
            }
        }
        return new Result(1, "success");
    }

    /**
     * 根据id获取菜谱，且返回用户信息
     *
     */
    @RequestMapping("/getRecipeById")
    public Result getRecipeById(Long id) {
        Recipe recipe = recipeService.getRecipeById(id);
        if (recipe == null) {
            return new Result(0, "暂无此菜谱");
        }
        return new Result(1, "success", recipe);
    }

    /**
     * 添加菜谱
     *
     *
     */
    @RequestMapping("/addRecipe")
    public Result addRecipe(Recipe r, MultipartFile[] myHeads) {
        StringBuilder s=new StringBuilder();
        for(int i=0;i<myHeads.length;i++){
            s.append("/api/upload/"+ UpDowns.upload(myHeads[i]));
            if(i!=myHeads.length-1)s.append(",");
        }
        r.setCoverImage(s.toString());
        int count = recipeService.addRecipe(r);
        if (count <= 0) {
            return new Result(0, "添加菜谱失败");
        }
        return new Result(1, "success");
    }
}
